package com.huawei.statistics.service.impl.member;


import com.huawei.statistics.common.service.wx.WXPlatform;
import com.huawei.statistics.dao.member.MemberDao;
import com.huawei.statistics.pojo.member.MemberType;
import com.huawei.statistics.pojo.member.User;
import com.huawei.statistics.redis.impl.common.RedisTemplate;
import com.huawei.statistics.schedule.job.MyQuartzJobBean;
import com.huawei.statistics.service.code.CodeService;
import com.huawei.statistics.service.impl.common.BaseServiceImpl;
import com.huawei.statistics.service.member.MemberService;
import com.huawei.statistics.service.schedule.quartz.ScheduleService;
import com.huawei.statistics.utils.DateTimeUtilT;
import com.huawei.statistics.utils.ObjectTranscoder;
import com.huawei.statistics.utils.PageUtil;
import com.huawei.statistics.vo.common.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by AB350 on 2017/11/28.
 */
@Service
public class MemberServiceImpl extends BaseServiceImpl implements MemberService {

	@Autowired
	MemberDao memberDao;
	@Autowired
	CodeService codeService;
	@Autowired
	HttpSession session;
	@Autowired
	WXPlatform wxPlatform;
	@Autowired
	RedisTemplate redisTemplate;
	@Autowired
	ScheduleService scheduleService;

	@Override
	public List<User> loadAllUser() {

		return loadAllEntity(User.class);
	}

	@Override
	public List<User> getUserBySpead(Long companyId) {

		Map<String,Object> map = new HashMap<>();
		map.put("companyId",companyId);
		map.put("spread",true);
		List<User> list = getEntityByMap(User.class,map);
		return list;
	}

	@Override
	public HttpResult getUserByMobile(String mobile,int page) {
		Map<String,Object> map = new HashMap<>();
		map.put("mobile",mobile);
		List<User> list = getEntityByMap(User.class,map);
		return new HttpResult(HttpResult.OK,new PageUtil(page,20,list));
	}

	@Override
	public HttpResult getUserByCondition(Map<String, Object> map,int page) {

		List<User> list = memberDao.getUserBycondition(map);
		return new HttpResult(HttpResult.OK,new PageUtil(page,20,list));
	}

	@Override
	public List<User> getUserByMobile(String mobile,Long companyId) {
		Map<String,Object> map = new HashMap<>();
		map.put("mobile",mobile);
		map.put("companyId",companyId);
		List<User> list = getEntityByMap(User.class,map);
		return list;
	}

	@Override
	public User getUserByCode(String code) {

		return getEntityByOneProperty(User.class,"userCode",code);
	}



	@Override
	public HttpResult getScanerByShop(Long shopId) {
		Map<String,Object> map = new HashMap<>();
		map.put("scanerShopId",shopId);
		map.put("scaner",true);
		List<User> list = getEntityByMap(User.class,map);
		return new HttpResult(HttpResult.OK,list);
	}



	@Override
	public void setMmeberGroup() {
		System.out.println("设置会员组开始========");
		List<MemberType> typeList = loadAllEntity(MemberType.class);
		System.out.println(typeList);
		List<User> list = loadAllEntity(User.class);
		Collections.sort(typeList,new Comparator<MemberType>(){
			@Override
			public int compare(MemberType o1, MemberType o2) {
				if(o1.getMinIntegral() < o2.getMinIntegral()){
					return -1;
				}else if(o1.getMinIntegral() == o2.getMinIntegral()){
					return 0;
				}else{
					return 1;
				}
			}
		});
		for(User user : list){
			System.out.println("用户本月会员"+user.getNick()+"积分"+user.getMonthIntegral()+"上月会员积分"+user.getLastMothIntegral());
			user.setLastMothIntegral(user.getMonthIntegral());
			user.setMonthIntegral(0);
			System.out.println("处理后用户本月会员"+user.getNick()+"积分"+user.getMonthIntegral()+"上月会员积分"+user.getLastMothIntegral());
			for(MemberType type : typeList){
				//System.out.println("main=="+type.getMinIntegral()+"max===="+type.getMaxIntegral());
				if(type.getMinIntegral()<=user.getLastMothIntegral()){
					user.setMemberGroup(type.getId());
				}
			}
			updateEntityValues(user);
		}

	}

	@Override
	public User getUserByOpenId(String openid) {
		return getEntityByOneProperty(User.class,"openid",openid);
	}

	@Override
	public HttpResult updateUserCover(User user) {

		Boolean b = saveOrUpdateEntity(user);
		String key ="user:"+user.getId();
		redisTemplate.set(key.getBytes(),ObjectTranscoder.serialize(user));

		return new HttpResult(HttpResult.OK,b);
	}

	@Override
	public List<User> getUserByMemberGroup(Long memberGroup) {

		Map<String,Object> map = new HashMap<>();
		map.put("memberGroup",memberGroup);
		List<User> list =getEntityByMap(User.class,map);
		return list;
	}



	@Override
	public List<User> getBindMobileUser(Long companyId) {

		Map<String,Object> map = new HashMap<>();
		map.put("companyId",companyId);
		List<User> list = getEntityByMap(User.class,map);
		if(list ==null)
			return null;
		List<User> result = new ArrayList<>();
		for(User user:list){
			if(user.getMobile() != null){
				if(!user.getMobile().equals("")){
					result.add(user);
				}

			}
		}
		return list;
	}


	@Override
	public Map<String, Object> getUserReturnMap(Long id) {

		Map<String,Object> map =  memberDao.findUserById(id);

		return map;
	}

	@Override
	public HttpResult getUserById(Long id) {

		Map<String,Object> map = memberDao.findUserById(id);
		return new HttpResult(HttpResult.OK,map);
	}
		
	
	/**
	 * 分页
	 * cur 当前页数
	 * max 每页条数
	 *
	 */

	@Override
	public HttpResult getUserList(Long companyId,int cur, int max) {

		Map<String,Object> map = new HashMap<>();
		map.put("companyId",companyId);
		List<User> list = findEntityByPage(User.class,map,cur,max,"createDate");
		for(User user:list){
			String key = "user:"+user.getId();
			redisTemplate.set(key.getBytes(),ObjectTranscoder.serialize(user));
		}

		return new HttpResult(HttpResult.OK,list);
	}


	@Override
	public HttpResult addUser(User user) {

		return new HttpResult(HttpResult.OK,addEntity(user));
	}


	@Override
	public HttpResult updateUser(User user) {
		System.out.println(user.getMobile());
		Boolean b = updateEntityValues(user);
		String key = "user:"+user.getId();
		redisTemplate.set(key.getBytes(),ObjectTranscoder.serialize(user));
		return new HttpResult(HttpResult.OK,b);
	}



	@Transactional
	@Override
	public Boolean registerMember(String recommenderOpenId, String entity) {

		System.out.println("保存用户信息:"+entity);
		return false;

	}

	@Override
	public User getUserByIdReturnUser(Long id) {

		return getEntityById(id, User.class);
	}


	/**
	 * 分页
	 * cur 当前页数
	 * max 每页条数
	 *
	 */

	@Override
	public HttpResult getUserListByAdmin(int cur, int max,Long companyId) {

		Map<String,Object> map = new HashMap<>();
		map.put("companyId",companyId);
		List<User> list = getEntitiesBySort(User.class,map,"id",true);
		if(list == null)
			return new HttpResult(HttpResult.NULL,"没有会员");

		for(User user:list){
			String key = "user:"+user.getId();
			redisTemplate.set(key.getBytes(),ObjectTranscoder.serialize(user));
		}

		PageUtil pageUtil = new PageUtil(cur,max,list);
		return new HttpResult(HttpResult.OK, pageUtil);
	}


	@Override
	public HttpResult getUserByNick(String nick,Long companyId) {
		Map<String,Object> map = new HashMap<>();
		map.put("companyId",companyId);
		List<User> list = getEntitiesBySort(User.class,map,"id",true);
		List<User> result = new ArrayList<>();
		if(list == null)
			return new HttpResult(HttpResult.NULL,"没有会员");
		for(int i = 0; i<list.size(); i++){
			if(list.get(i).getNick().contains(nick)){
				result.add(list.get(i));
			}
		}
		return new HttpResult(HttpResult.OK,result);
	}


	@Override
	public HttpResult getUserByName(String name, Long companyId) {

		Map<String,Object> map = new HashMap<>();
		map.put("companyId",companyId);
		List<User> list = getEntitiesBySort(User.class,map,"id",true);
		List<User> result = new ArrayList<>();
		if(list == null)
			return new HttpResult(HttpResult.NULL,"没有会员");
		for(int i = 0; i<list.size(); i++){
			if(list.get(i).getRealName().contains(name)){
				result.add(list.get(i));
			}
		}
		return new HttpResult(HttpResult.OK,result);
	}

	@Override
	public HttpResult getUserByMoble(String mobile, Long companyId) {
		Map<String,Object> map = new HashMap<>();
		map.put("companyId",companyId);
		List<User> list1 = getEntitiesBySort(User.class,map,"id",true);
		List<User> result = new ArrayList<>();
		if(list1 == null)
			return new HttpResult(HttpResult.NULL,"没有会员");
		for(int i = 0; i<list1.size(); i++){
			if(list1.get(i).getMobile().contains(mobile)){
				result.add(list1.get(i));
			}
		}
		return new HttpResult(HttpResult.OK,result);
	}
}
